package order_service

import (
	"bee/pkg/logging"
	"bee/pkg/util"
	"github.com/go-resty/resty/v2"
)

type SettleProduct struct {
	ProductID        string `json:"productId"`
	ProductVersionID string `json:"productVersionId"`
}

type SettleOrderProductInfo struct {
	SettleProduct  `json:"settleProduct"`
}

type SettleOrderContactInfo struct {
	ContactName string      `json:"contactName"`
	Gender      string      `json:"gender"`
	GenderName  string      `json:"genderName"`
	Role        string      `json:"role"`
	Mobile      string      `json:"mobile"`
	Phone       string      `json:"phone"`
	Email       interface{} `json:"email"`
	MobileID    string      `json:"mobileId"`
}

type SettleOrderOrderRemark struct {
	SignerID     string `json:"signerId"`
	OperatorType string `json:"operatorType"`
	OperatorID   string `json:"operatorId"`
}

type SettleOrder struct {
	BaseInfo  `json:"baseInfo"`
	SettleOrderProductInfo  `json:"productInfo"`
	SettleOrderContactInfo  `json:"contactInfo"`
	SettleOrderOrderRemark  `json:"orderRemark"`
}

func (o Order) CreateSettleOrder(host string) (string, error) {

	url := host + "/api/otcpro/v1/order/settleOrder/save"

	var productInfo SettleOrderProductInfo

	// 0代表不下单，1代表只下预付费订单，2代表一个预付费订单和一个阶梯单价后付费，3代表一个预付费订单和一个固定单价后付费
	if o.OrderType == 0 || o.OrderType == 1 {
		logging.ZapLogger.Infow("【后付费订单】 下单类型为0或者1，不下后付费订单", "orderType", o.OrderType,
			"traceId", o.TraceId)
		return "", nil
	}

	productInfo.SettleProduct.ProductID = "32578"
	productInfo.SettleProduct.ProductVersionID = "56140"

	if o.OrderType == 2 {
		productInfo.SettleProduct.ProductID = "32578"
		productInfo.SettleProduct.ProductVersionID = "56140"
	}

	if o.OrderType == 3 {
		productInfo.SettleProduct.ProductID = "32577"
		productInfo.SettleProduct.ProductVersionID = "56130"
	}

	orderType := "0"
	baseInfo := BaseInfo{
		OrderType:    orderType,
		PurchaserID:  o.CustomerId,
		ServiceType:  "01",
		SalesChannel: "50",
		TerminalType: "1",
	}

	contactInfo := SettleOrderContactInfo{
		ContactName: o.FullName,
		Gender:      "0",
		GenderName:  "男",
		Role:        "",
		Mobile:      "",
		Phone:       "",
		Email:       nil,
		MobileID:    "",
	}

	orderRemark := SettleOrderOrderRemark{
		SignerID:     "1B2C20B854FE4B588A87F4CABE6DEF6D",
		OperatorType: "2",
		OperatorID:   "1B2C20B854FE4B588A87F4CABE6DEF6D",
	}

	settleOrder := SettleOrder{
		BaseInfo:               baseInfo,
		SettleOrderProductInfo: productInfo,
		SettleOrderContactInfo: contactInfo,
		SettleOrderOrderRemark: orderRemark,
	}
	cookie := util.SetCookie("sso-epctoken", o.Token)
	headers := make(map[string]string)
	headers["Content-Type"] = "application/json;charset=UTF-8"

	logging.ZapLogger.Infow("后付费订单下单", "下单数据", settleOrder, "traceId", o.TraceId)
	client := resty.New()
	resp, ordinaryOrderErr := client.R().EnableTrace().
		SetCookie(cookie).
		SetHeaders(headers).SetBody(settleOrder).
		Post(url)

	if ordinaryOrderErr != nil {
		return "", ordinaryOrderErr
	}

	logging.ZapLogger.Infow("后付费下单结果", "data", resp.String(), "traceId", o.TraceId)

	return resp.String(), nil
}